﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YLW.Plat.Jhfw.HL7V3.XSDModels.PRPA_IN900350UV_Zyzk;
using YLW.Plat.Jhfw.Model.JhfwZyzk;

namespace YLW.Plat.Jhfw.HL7V3.V3Message
{
    public class V3_PRPA_IN900350UV_Zyzk
    {
        public static string TransferInfoQuery_Response(GetJhfwZyzkListOut jsonData, string id_extension, string sender_name, string receiver_name)
        {
            string hl7Xml = string.Empty;

            if (jsonData == null)
            {
                return hl7Xml;
            }

            //根据XML XSD构建HL7 XML消息格式

            //创建根对象
            var xmlRoot = new PRPA_IN900350UV
            {
                ITSVersion = "XML_1.0",
                id = new id { root = "2.16.156.10011.2.5.1.1", extension = Guid.NewGuid().ToString() },
                creationTime = new creationTime { value = DateTime.Now.ToString("yyyyMMddHHmmss") },
                interactionId = new interactionId { root = "2.16.156.10011.2.5.1.2", extension = "PRPA_IN900350UV" },
                processingCode = new processingCode { code = "P" },
                processingModeCode = new processingModeCode(),
                acceptAckCode = new acceptAckCode { code = "AL" },
                receiver = new receiver
                {
                    typeCode = "RCV",
                    device1 = new device1
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id = new id
                        {
                            root = "2.16.156.10011.2.5.1.3",
                            extension = receiver_name
                        }
                    }
                },
                sender = new sender
                {
                    typeCode = "SND",
                    device1 = new device1
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id = new id
                        {
                            root = "2.16.156.10011.2.5.1.3",
                            extension = sender_name
                        }
                    }
                },
                acknowledgement = new acknowledgement
                {
                    typeCode = jsonData.subcode == "success" ? "AA" : "AE",
                    targetMessage = new targetMessage
                    {
                        id = new id
                        {
                            root = "2.16.156.10011.2.5.1.1",
                            extension = id_extension
                        }
                    },
                    acknowledgementDetail = new acknowledgementDetail
                    {
                        text = new text
                        {
                            value = jsonData.submsg
                        }
                    }
                },
                controlActProcess = new controlActProcess
                {
                    classCode = "ACTN",
                    moodCode = "PRMS",

                    queryAck = new queryAck
                    {
                        queryId = new queryId { extension = "" },
                        queryResponseCode = new queryResponseCode { code = jsonData.subcode == "success" ? "OK" : "error" },
                    }
                },
            };

            List<subject> subList = new List<subject>();

            if (jsonData != null && jsonData.data != null)
            {
                xmlRoot.controlActProcess.queryAck.resultTotalQuantity = new resultTotalQuantity { value = jsonData.data.Count.ToString() };

                foreach (var listItem in jsonData.data)
                {
                    subList.Add(new subject
                    {
                        typeCode = "SUBJ",
                        encounterEvent = new encounterEvent
                        {
                            classCode = "ENC",
                            moodCode = "EVN",
                            id = new id
                            {
                                item = new item[] {
                                    new item{
                                        root = "2.16.156.10011.1.12",
                                        extension = ""
                                    },
                                    new item{
                                        root = "2.16.156.10011.2.5.1.8",
                                        extension = listItem.series
                                    },
                                    new item{
                                        root = "2.16.156.10011.2.5.1.9",
                                        extension = listItem.jzlsh
                                    }
                                }
                            },
                            code = new code
                            {
                                code1 = "3",
                                codeSystem = "2.16.156.10011.2.3.1.271",
                                codeSystemName = "患者类型代码表",
                                displayName = new displayName { value = "住院" }
                            },
                            statusCode = new statusCode(),
                            effectiveTime = new effectiveTime
                            {
                                low = new low
                                {
                                    value = listItem.zyrq
                                }
                            },
                            subject = new subject
                            {
                                typeCode = "SBJ",
                                patient = new patient
                                {
                                    classCode = "PAT",
                                    id = new id
                                    {
                                        item = new item[]
                                        {
                                            new item
                                            {
                                                root = "2.16.156.10011.2.5.1.4",
                                                extension = listItem.mrn
                                            }
                                        }
                                    },
                                    patientPerson = new patientPerson
                                    {
                                        id = new id
                                        {
                                            item = new item[]
                                            {
                                                new item
                                                {
                                                    root = "2.16.156.10011.2.5.1.3",
                                                    extension = listItem.pcid
                                                }
                                            }
                                        },
                                        name = new name
                                        {
                                            item = new item
                                            {
                                                part = new part
                                                {
                                                    value = listItem.pname
                                                }
                                            }
                                        }
                                    }
                                }
                            },
                            admitter = new admitter
                            {
                                typeCode = "ADM",
                                time = new time(),
                                assignedPerson = new assignedPerson
                                {
                                    classCode = "",
                                    id = new id
                                    {
                                        item = new item[]
                                        {
                                            new item
                                            {
                                                root = "2.16.156.10011.1.4",
                                                extension = listItem.zyysdm
                                            }
                                        }
                                    },
                                    assignedPerson1 = new assignedPerson
                                    {
                                        classCode = "PSN",
                                        determinerCode = "INSTANCE",
                                        name = new name
                                        {
                                            item = new item
                                            {
                                                part = new part
                                                {
                                                    value = listItem.zyysxm
                                                }
                                            }
                                        }
                                    }
                                }
                            },
                            location = new location[] {
                                new location
                                {
                                    typeCode = "ORG",
                                    time = new time
                                    {
                                        low = new low
                                        {
                                            value = listItem.zr_date
                                        }
                                    },
                                    statusCode = new statusCode(),
                                    serviceDeliveryLocation = new serviceDeliveryLocation
                                    {
                                        classCode = "SDLOC",
                                        location = new location
                                        {
                                            classCode = "PLC",
                                            determinerCode = "INSTANCE",
                                            id = new id
                                            {
                                                item = new item[]
                                                {
                                                    new item
                                                    {
                                                        root = "2.16.156.10011.1.26",
                                                        extension = listItem.zrksdm
                                                    }
                                                }
                                            },
                                            name = new name
                                            {
                                                item = new item
                                                {
                                                    part = new part
                                                    {
                                                        value = listItem.zrksmc
                                                    }
                                                }
                                            },
                                            locatedEntityHasParts = new locatedEntityHasParts
                                            {
                                                locatedPlace = new locatedPlace[]
                                                {
                                                    new locatedPlace{
                                                        Items=new object[]{
                                                            new id
                                                            {
                                                                root = "2.16.156.10011.1.27",
                                                                extension = listItem.zrbqdm
                                                            },
                                                            new name{
                                                                item= new item
                                                                {
                                                                    part = new part
                                                                    {
                                                                        value = listItem.zrbqmc
                                                                    }
                                                                }
                                                            },
                                                            new locatedEntityHasParts
                                                            {
                                                                locatedPlace = new locatedPlace[]
                                                                {
                                                                    new locatedPlace{
                                                                        Items=new object[]{
                                                                            new id
                                                                            {
                                                                                root = "2.16.156.10011.1.21",
                                                                                extension = listItem.zrbf
                                                                            },
                                                                            new name{
                                                                                item= new item
                                                                                {
                                                                                    part = new part
                                                                                    {
                                                                                        value = listItem.zrbf
                                                                                    }
                                                                                }
                                                                            },
                                                                            new locatedEntityHasParts
                                                                            {
                                                                                locatedPlace = new locatedPlace[]
                                                                                {
                                                                                    new locatedPlace{
                                                                                        Items=new object[]{
                                                                                            new id
                                                                                            {
                                                                                                root = "2.16.156.10011.1.22",
                                                                                                extension = listItem.zrbc
                                                                                            },
                                                                                            new name{
                                                                                                item= new item
                                                                                                {
                                                                                                    part = new part
                                                                                                    {
                                                                                                        value = listItem.zrbc
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                },new location
                                {
                                    typeCode = "DST",
                                    time = new time
                                    {
                                        low = new low
                                        {
                                            value = listItem.zc_date
                                        }
                                    },
                                    statusCode = new statusCode(),
                                    serviceDeliveryLocation = new serviceDeliveryLocation
                                    {
                                        classCode = "SDLOC",
                                        location = new location
                                        {
                                            classCode = "PLC",
                                            determinerCode = "INSTANCE",
                                            id = new id
                                            {
                                                item = new item[]
                                                {
                                                    new item
                                                    {
                                                        root = "2.16.156.10011.1.26",
                                                        extension = listItem.zcksdm
                                                    }
                                                }
                                            },
                                            name = new name
                                            {
                                                item = new item
                                                {
                                                    part = new part
                                                    {
                                                        value = listItem.zcksmc
                                                    }
                                                }
                                            },
                                            locatedEntityHasParts = new locatedEntityHasParts
                                            {
                                                locatedPlace = new locatedPlace[]
                                                {
                                                    new locatedPlace{
                                                        Items=new object[]{
                                                            new id
                                                            {
                                                                root = "2.16.156.10011.1.27",
                                                                extension = listItem.zcbqdm
                                                            },
                                                            new name{
                                                                item= new item
                                                                {
                                                                    part = new part
                                                                    {
                                                                        value = listItem.zcbqmc
                                                                    }
                                                                }
                                                            },
                                                            new locatedEntityHasParts
                                                            {
                                                                locatedPlace = new locatedPlace[]
                                                                {
                                                                    new locatedPlace{
                                                                        Items=new object[]{
                                                                            new id
                                                                            {
                                                                                root = "2.16.156.10011.1.21",
                                                                                extension = listItem.zcbf
                                                                            },
                                                                            new name{
                                                                                item= new item
                                                                                {
                                                                                    part = new part
                                                                                    {
                                                                                        value = listItem.zcbf
                                                                                    }
                                                                                }
                                                                            },
                                                                            new locatedEntityHasParts
                                                                            {
                                                                                locatedPlace = new locatedPlace[]
                                                                                {
                                                                                    new locatedPlace{
                                                                                        Items=new object[]{
                                                                                            new id
                                                                                            {
                                                                                                root = "2.16.156.10011.1.22",
                                                                                                extension = listItem.zcbc
                                                                                            },
                                                                                            new name{
                                                                                                item= new item
                                                                                                {
                                                                                                    part = new part
                                                                                                    {
                                                                                                        value = listItem.zcbc
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            },
                            extension = new extension
                            {
                                sjly = new value { value1 = listItem.sjly },
                                lyxt = new value { value1 = listItem.lyxt },
                                hoscode = new value { value1 = listItem.hoscode },
                                zkid = new value { value1 = listItem.zkid },
                                jzlsh = new value { value1 = listItem.jzlsh },
                                mrn = new value { value1 = listItem.mrn },
                                pname = new value { value1 = listItem.pname },
                                pcid = new value { value1 = listItem.pcid },
                                series = new value { value1 = listItem.series },
                                stay = new value { value1 = listItem.stay },
                                zyrq = new value { value1 = listItem.zyrq },
                                zyysdm = new value { value1 = listItem.zyysdm },
                                zyysxm = new value { value1 = listItem.zyysxm },
                                zrksdm = new value { value1 = listItem.zrksdm },
                                zrksmc = new value { value1 = listItem.zrksmc },
                                zrbqdm = new value { value1 = listItem.zrbqdm },
                                zrbqmc = new value { value1 = listItem.zrbqmc },
                                zrbc = new value { value1 = listItem.zrbc },
                                zrbf = new value { value1 = listItem.zrbf },
                                zr_date = new value { value1 = listItem.zr_date },
                                zcksdm = new value { value1 = listItem.zcksdm },
                                zcksmc = new value { value1 = listItem.zcksmc },
                                zcbqdm = new value { value1 = listItem.zcbqdm },
                                zcbqmc = new value { value1 = listItem.zcbqmc },
                                zcbc = new value { value1 = listItem.zcbc },
                                zcbf = new value { value1 = listItem.zcbf },
                                zc_date = new value { value1 = listItem.zc_date },
                                useflag = new value { value1 = listItem.useflag },
                                in_empid = new value { value1 = listItem.in_empid },
                                in_date = new value { value1 = listItem.in_date },
                                xg_empid = new value { value1 = listItem.xg_empid },
                                xgsj = new value { value1 = listItem.xgsj }
                            }
                        }
                    });
                }
            }

            xmlRoot.controlActProcess.subject = subList.ToArray();

            hl7Xml = HL7V3Helper.SerializeToXml(xmlRoot);

            return hl7Xml;
        }
    }
}
